package session

import (
	"bytes"
	"encoding/gob"
)

func init() {
	gob.Register([]interface{}{})
	gob.Register(map[int]int{})
	gob.Register(map[int]string{})
	gob.Register(map[string]int{})
	gob.Register(map[string]string{})
	gob.Register(map[int]interface{}{})
	gob.Register(map[string]interface{}{})
	gob.Register(map[interface{}]interface{}{})
}

func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) {
	for _, v := range obj {
		gob.Register(v)
	}
	buff := bytes.NewBuffer(nil)
	encoder := gob.NewEncoder(buff)
	err := encoder.Encode(obj)
	if err != nil {
		return []byte(""), err
	}
	return buff.Bytes(), nil
}

func DecodeGob(encoded []byte) (map[interface{}]interface{}, error) {
	buff := bytes.NewBuffer(encoded)
	decoder := gob.NewDecoder(buff)
	var result map[interface{}]interface{}
	err := decoder.Decode(&result)
	if err != nil {
		return nil, err
	}
	return result, nil
}
